﻿using System;
using Microsoft.SharePoint;
using SharePoint.Orm.Helpers;

namespace SharePoint.Orm.SiteBuilders {
	/// <summary>
	/// Provides the ability to find/add a field lookup that can reference another field.
	/// </summary>
	public class FieldLookupBuilder : BuilderBase<SPFieldLookup> {
		/// <summary>
		/// The Guid in string format that the field will be located by.
		/// </summary>
		public string GuidStr { get; set; }
		
		/// <summary>
		/// Required.  A group to put the field in.
		/// </summary>
		public static string GroupName { get; set; }

		/// <summary>
		/// Required.  The name of the field.
		/// </summary>
		public string Name { get; set; }
		
		/// <summary>
		/// DisplayName is optional because name should generally be both display name and identifier.
		/// You should only use DisplayName if there is a conflict when using Name.
		/// </summary>
		public string DisplayName { get; set; }

		/// <summary>
		/// Optional, default = "Lookup".
		/// </summary>
		public string FieldTypeString { get; set; }

		/// <summary>
		/// Required.  The parent list the one you're creating will refer to.
		/// </summary>
		public SPList List { get; set; }

		/// <summary>
		/// Optional, default = true, but totally worth being explicit about.
		/// </summary>
		[Obsolete("Use IsRequired instead.")]
		public bool Required { get; set; }
        /// <summary>
        /// Optional, default = true, but totally worth being explicit about.
        /// </summary>
        public bool IsRequired { get; set; }
		/// <summary>
		/// Optional, default = "Title".  The field of the parent list that will be shown to users and displayed in dropdowns.
		/// </summary>
		public string LookupField { get; set; }

		/// <summary>
		/// Optional, default SPFieldType.Text.  This is mutually exclusive with the FieldTypeString property, you should only use one.
		/// </summary>
		public SPFieldType FieldType { get; set; }

		/// <summary>
		/// Initializes default properties of fields (see individual field descriptions).
		/// </summary>
		public FieldLookupBuilder() {
			Required = true;
		    IsRequired = true;
			LookupField = null;
			DisplayName = null;
			FieldTypeString = "Lookup";
		}

		public override SPFieldLookup FindAdd() {
			if (Site == null) throw new SPOrmException("Site is required.");
			if (string.IsNullOrEmpty(GroupName)) throw new SPOrmException("GroupName is required.");
			if (string.IsNullOrEmpty(Name)) throw new SPOrmException("Name is required.");
			if (List == null) throw new SPOrmException("List is required.");

			SPFieldLookup fieldLookup = SPWebHelper.FindOrAddFieldLookup(Site, new Guid(GuidStr), GroupName, Name, FieldTypeString, List.ID, List.ParentWeb.ID, Required && IsRequired);

			if (!string.IsNullOrEmpty(DisplayName) && DisplayName != Name) {
				fieldLookup.Title = DisplayName;
				fieldLookup.Update();
			}

			string lookupField = LookupField ?? "Title";
			fieldLookup.LookupField = lookupField;
			fieldLookup.Update();

			return fieldLookup;
		}
	}
}
